//
//  BLNavigationController.m
//  EClub
//
//  Created by Hays on 16/5/30.
//  Copyright © 2016年 EC. All rights reserved.
//

#import "BLNavigationController.h"
#import "UIColor+Create.h"

@interface BLNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@end

@implementation BLNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configNavBar];
}

#pragma mark - navigation bar ui

- (void)configNavBar
{
    self.navigationBar.barTintColor = [UIColor colorWithHexValue:0x030303];
    self.navigationBar.tintColor = [UIColor colorWithHexValue:0x030303];
    self.navigationBar.translucent = NO;
    self.navigationBar.exclusiveTouch = YES;   // 避免返回按钮和其它按钮可以同时点击
    self.supportPortraitOnly = YES;
    [self.navigationBar setBackgroundImage:[[UIImage alloc] init]
                                      forBarPosition:UIBarPositionAny
                                          barMetrics:UIBarMetricsDefault];
    [self.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithHexValue:0xffffff], NSForegroundColorAttributeName, [UIFont systemFontOfSize:16.0], NSFontAttributeName, nil]];
}

#pragma mark - orientations

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.supportPortraitOnly) {
        return UIInterfaceOrientationMaskPortrait;
    } else {
        if ([self.topViewController isKindOfClass:[UITabBarController class]]) {
            return [[(UITabBarController *)self.topViewController selectedViewController] supportedInterfaceOrientations];
        } else {
            return [self.topViewController supportedInterfaceOrientations];
        }
    }
}

- (BOOL)shouldAutorotate {
    if (self.supportPortraitOnly) {
        return NO;
    } else {
        if ([self.topViewController isKindOfClass:[UITabBarController class]]) {
            return [[(UITabBarController *)self.topViewController selectedViewController] shouldAutorotate];
        } else {
            return [self.topViewController shouldAutorotate];
        }
    }
}

//- (void)setNeedsStatusBarAppearanceUpdate {
//    [super setNeedsStatusBarAppearanceUpdate];
//}
//
//- (UIStatusBarStyle)preferredStatusBarStyle {
//    return self.topViewController.preferredStatusBarStyle;
//}
//

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.topViewController.preferredStatusBarStyle;
}

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

/*
#pragma mark -  override

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (animated) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
    if (animated) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    return  [super popToRootViewControllerAnimated:animated];
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (animated) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    return [super popToViewController:viewController animated:animated];
}

#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    self.interactivePopGestureRecognizer.enabled = YES;
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        if (self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
            return NO;
        }
    }
    
    return YES;
}
*/
@end

